/*
   Copyright 2011-2012 gtalent2@gmail.com

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

     http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/
package world

import (
	"github.com/gtalent/starfish/gfx"
	"github.com/gtalent/starfish/input"
	"github.com/gtalent/starfish/util"
)

type Camera struct {
	util.Bounds
	world       *World
	focusSprite Sprite
}

func NewCamera(world *World) *Camera {
	c := new(Camera)
	input.AddKeyPressListener(c)
	input.AddKeyReleaseListener(c)
	c.world = world
	return c
}

func (me *Camera) Free() {
	input.RemoveKeyPressListener(me)
	input.RemoveKeyReleaseListener(me)
	me.world = nil
	me.focusSprite = nil
}

func (me *Camera) KeyPress(e input.KeyEvent) {
	s := me.focusSprite
	switch s.(type) {
	case *User:
		s.(*User).KeyPress(e)
	}
}

func (me *Camera) KeyRelease(e input.KeyEvent) {
	s := me.focusSprite
	switch s.(type) {
	case *User:
		s.(*User).KeyRelease(e)
	}
}

func (me *Camera) SetFocusSprite(sprite Sprite) {
	me.focusSprite = sprite
}

func (me *Camera) SetWorld(world *World) {
	me.world = world
}

func (me *Camera) Draw(c *gfx.Canvas) {
	c.SetColor(gfx.Color{0, 0, 0, 255})
	c.PushViewport(0, 0, me.Width, me.Height)
	c.FillRect(0, 0, me.Width, me.Height)
	world := me.world
	if world == nil {
		return
	}
	focusSprite := me.focusSprite
	bounds := me.Bounds
	point := &bounds.Point
	for _, z := range world.zones {
		for layer := 0; layer < z.Layers(); layer++ {
			if z.Intersects(me.Bounds) {
				if !z.loaded && z.load() != nil {
					continue
				}

				var focusPoint util.Point
				if focusSprite != nil {
					//has focusSpirte
					size := me.Size.DivideOf(util.Size{2, 2})
					focusPoint = size.ToPoint()
					*point = focusSprite.Location().SubtractOf(me.Size.ToPoint().DivideOf(util.Point{X: 2, Y: 2}))
					point.AddTo(tileSize.ToPoint().DivideOf(util.Point{2, 2}))
					focusPoint.SubtractFrom(tileSize.ToPoint().DivideOf(util.Point{2, 2}))
				}

				zb := z.GetBounds()
				p1 := bounds.Point.SubtractOf(zb.Point)
				p2 := bounds.Point2().SubtractOf(zb.Point)

				//calculate the bounds of the Tiles to draw

				//makes sure no values are less than 0
				if p1.X < 0 {
					p1.X = 0
				}
				if p1.Y < 0 {
					p1.Y = 0
				}

				p2.AddTo(tileSize.ToPoint())
				//makes sure no values are beyond the bounds of the Zone
				if p2.X >= zb.Width {
					p2.X = zb.Width
				}
				if p2.Y >= zb.Height {
					p2.Y = zb.Height
				}
				p1.DivideBy(tileSize.ToPoint())
				p2.DivideBy(tileSize.ToPoint())

				//draw lower layers
				p := p1.MultiplyOf(tileSize.ToPoint())
				for y := p1.Y; y < p2.Y; y++ {
					p.X = p1.X * tileSize.Width
					for x := p1.X; x < p2.X; x++ {
						t := z.getTile(x, y, layer)
						t.drawLowerLayers(c, p.SubtractOf(*point))
						p.X += tileSize.Width
					}
					p.Y += tileSize.Height
				}
				//draw sprites
				p = p1.MultiplyOf(tileSize.ToPoint())
				for y := p1.Y; y < p2.Y; y++ {
					p.X = p1.X * tileSize.Width
					for x := p1.X; x < p2.X; x++ {
						t := z.getTile(x, y, layer)
						t.drawSprite(c, p, p.SubtractOf(*point), t.occupant == focusSprite, focusPoint)
						p.X += tileSize.Width
					}
					p.Y += tileSize.Height
				}
				//draw upper layers
				p = p1.MultiplyOf(tileSize.ToPoint())
				for y := p1.Y; y < p2.Y; y++ {
					p.X = p1.X * tileSize.Width
					for x := p1.X; x < p2.X; x++ {
						t := z.getTile(x, y, layer)
						t.drawUpperLayers(c, p.SubtractOf(*point))
						p.X += tileSize.Width
					}
					p.Y += tileSize.Height
				}
			}
		}
	}
	c.PopViewport()
}
